package com.lcz.authority.admin.mapper;

import com.lcz.authority.common.core.mybatis.BaseMapperX;
import com.lcz.authority.common.core.mybatis.LambdaQueryWrapperX;
import com.lcz.authority.common.core.mybatis.MPJLambdaWrapperX;
import com.lcz.authority.common.core.page.PageResult;
import com.lcz.authority.common.domain.entity.SysResourceEntity;
import com.lcz.authority.common.domain.entity.SysRoleEntity;
import com.lcz.authority.common.domain.entity.SysRoleResourceEntity;
import com.lcz.authority.common.domain.entity.SysUserRoleEntity;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author lcz
 * @desc 系统资源映射器
 * @mail lcz_0130@163.com
 * @date 2024/05/06
 */

public interface SysResourceMapper extends BaseMapperX<SysResourceEntity> {
    default PageResult<SysResourceEntity> selectPage(SysResourceEntity resource) {
        return selectPage(new LambdaQueryWrapperX<SysResourceEntity>()
                .likeIfPresent(SysResourceEntity::getAppCode, resource.getAppCode())
                .likeIfPresent(SysResourceEntity::getModularName, resource.getModularName())
                .likeIfPresent(SysResourceEntity::getHttpMethod, resource.getHttpMethod())
                .betweenIfPresent(SysResourceEntity::getCreateTime, resource.getParams()));
    }


    /**
     * 清空 sys_resource 数据库
     */
    @Update("truncate table sys_resource")
    void truncateResource();


    /**
     * 根据用户查询api资源列表
     *
     * @param resource
     * @return
     */
    default List<SysResourceEntity> selectResourceListByUserId(SysResourceEntity resource) {
        MPJLambdaWrapperX<SysResourceEntity> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.selectAll(SysResourceEntity.class);
        wrapperX.leftJoin(SysResourceEntity.class, "resource_code", SysResourceEntity::getResourceCode, SysRoleResourceEntity::getResourceCode);
        wrapperX.leftJoin(SysRoleResourceEntity.class, "role_id", SysRoleResourceEntity::getRoleId, SysUserRoleEntity::getRoleId);
        wrapperX.leftJoin(SysUserRoleEntity.class, "role_id", SysUserRoleEntity::getRoleId, SysRoleEntity::getRoleId);
        wrapperX.eqIfExists(SysResourceEntity::getResourceCode, resource.getResourceCode());
        wrapperX.eqIfExists(SysResourceEntity::getClassName, resource.getClassName());
        wrapperX.eqIfExists(SysResourceEntity::getRequiredPermissionFlag, resource.getRequiredPermissionFlag());
        return selectList(wrapperX);
    }

    /**
     * 查询资源列表
     *
     * @param resource
     * @return
     */
    default List<SysResourceEntity> selectResourceList(SysResourceEntity resource) {
        return selectList(new LambdaQueryWrapperX<SysResourceEntity>()
                .eqIfPresent(SysResourceEntity::getResourceCode, resource.getResourceCode())
                .eqIfPresent(SysResourceEntity::getClassName, resource.getClassName())
                .eqIfPresent(SysResourceEntity::getRequiredPermissionFlag, resource.getRequiredPermissionFlag()));
    }


}
